package com.kanq.ocr.config;

import com.kanq.ocr.service.DocumentRecognitionService;
import com.kanq.ocr.service.impl.DocumentRecognitionServiceImpl;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.concurrent.Executor;

/**
 * @author wangchong
 */
@Configuration
@EnableConfigurationProperties(OcrAiProperties.class)
@Import(ThreadPoolConfig.class)  // Add this line
public class OcrAiAutoConfiguration {

    @Bean
    public OpenAiApi ollamaApi(OcrAiProperties properties) {
        return new OpenAiApi(properties.getAi().getBaseUrl(), properties.getAi().getApiKey());
    }

    @Bean
    public OpenAiChatModel ollamaChatModel(OpenAiApi openAiApi, OcrAiProperties properties) {
        OpenAiChatOptions build = OpenAiChatOptions.builder().model(properties.getAi().getModel()).temperature(Double.valueOf(properties.getAi().getTemperature())).build();
        return OpenAiChatModel.builder().openAiApi(openAiApi).defaultOptions(build).build();
    }

    @Bean
    public PromptTemplates promptTemplates() {
        return new PromptTemplates();
    }

    @Bean
    public DocumentRecognitionService documentRecognitionService(OcrAiProperties ocrAiProperties, OpenAiChatModel openAiChatModel, PromptTemplates promptTemplates, @Qualifier("ocrTaskExecutor") Executor ocrTaskExecutor) {
        return new DocumentRecognitionServiceImpl(ocrAiProperties, openAiChatModel, promptTemplates, ocrTaskExecutor);
    }
}